package com.sendi.mobilework;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.sendi.mobilework.pager.BasePager;
import com.sendi.mobilework.pager.ext.CompanyPager;
import com.sendi.mobilework.pager.ext.ContactBookPager;
import com.sendi.mobilework.pager.ext.WorkArrangePager;
import com.sendi.mobilework.pager.ext.WorkflowManagerPager;
import com.sendi.mobilework.pager.ext.WorkflowWorkerPager;
import com.sendi.mobilework.view.NotScrollViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private NotScrollViewPager mViewPager;
    private RadioGroup mRadioGroup;
    private List<BasePager> mPagerList;

    private String mIdentity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        mIdentity=getIntent().getStringExtra("identity");
mIdentity="0";
        initView();
        iniData();
    }

    /**
     * 初始化数据
     */
    private void iniData() {
        mPagerList = new ArrayList<>();
        mPagerList.add(new ContactBookPager(this));
        mPagerList.add(new WorkArrangePager(this));
        mPagerList.add(new CompanyPager(this));
        if (mIdentity.equals("1")){
            mPagerList.add(new WorkflowWorkerPager(this));
        }else {
            mPagerList.add(new WorkflowManagerPager(this));
        }


        mViewPager.setAdapter(new ContentAdapter());

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //先初始化数据
                mPagerList.get(position).initData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_contacts:
                        mViewPager.setCurrentItem(0,false);//设置当前页
                        break;
                    case R.id.rb_flow:
                        mViewPager.setCurrentItem(1,false);
                        break;
                    case R.id.rb_news:
                        mViewPager.setCurrentItem(2,false);
                        break;
                    case R.id.rb_arrange:
                        mViewPager.setCurrentItem(3,false);
                        break;
                    default:break;
                }
            }
        });

        mRadioGroup.check(R.id.rb_contacts);//默认选中第一个界面
    }

    /**
     * 初始化界面
     */
    private void initView() {
        mViewPager = (NotScrollViewPager) findViewById(R.id.viewpager);
        mRadioGroup = (RadioGroup) findViewById(R.id.rg_content);
    }

    class ContentAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mPagerList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager pager = mPagerList.get(position);
            container.addView(pager.mRootView);
            pager.initData();
            return pager.mRootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
